<?php

require_once $_SESSION["root_path"] . "/DB/database.inc";
global $app_fields;
global $app_display_names;
/* list of fields that can be displayed
 * when it references an ID, should end with # followed by the field of the referenced table
 * example: applicant.ngo_id#name 
 *          means it corresponds to the field 'applicant.ngo_id' 
 *          but we want to display the value of field 'name' of the referenced table
 */
$app_display_names = array(
    "Identity" => array(
        "applicant.applicant_id" => "DB ID",
        "applicant.applicant_selection_id" => "ID",
        "applicant.applicant_firstname" => "First Name",
        "applicant.applicant_lastname" => "Last Name",
        "applicant.applicant_khmer_name" => "Khmer Name",
        "applicant.sex" => "Sex",
        "applicant.applicant_date_birth" => "Date of Birth",
        "applicant.applicant_age" => "Age",
        "applicant.applicant_origin_province_id#province_name" => "Origin of the student",
        "applicant.applicant_parents_province_id#province_name" => "Origin of the parents",
        "applicant.high_school_id#high_school_name" => "High School",
        "applicant.ngo_id#ngo_name" => "NGO",
        "applicant.comments" => "Comments",
        "applicant.excluded" => "Excluded",
        "applicant.excluded_reason" => "Exclusion reason",
    ),
    "Social Indications" => array(
        "applicant_more_info.family_revenue" => "Family Revenue /month",
        "applicant_more_info.family_expense" => "Family Expense /month",
        "applicant_more_info.family_members" => "Number of members",
        "applicant_more_info.number_of_married_and_seperated_member" => "Number seperated family",
        "applicant_more_info.number_of_children_only_living_in_family" => "Number only living in family",
        "applicant_more_info.farming_land_owned" => "Farming land owned",
        "applicant_more_info.properties_owned" => "Properties owned",
        "applicant_more_info.animal_owned" => "Animal owned",
    ),
    "Written Exam" => array(
        "applicant_exam_room_beta.exam_center_id#province_id#province_name" => "Exam Center Province",
        "applicant_exam_room_beta.exam_center_id#exam_center_name" => "Exam Center",
        "applicant_exam_room_beta.room_number" => "Exam Room Number",
        "applicant_exam_room_beta.exam_id#exam_date" => "Exam Date",
        "applicant_exam_room_beta.exam_id#exam_start_time" => "Exam Start Time",
        "applicant_exam_room_beta.exam_id#exam_end_time" => "Exam End Time",
        "applicant_more_info.applicant_more_info_attendee_exam_status" => "Exam Attendance",
    ),
    "Exam Results" => array(
    ),
    "Eligibility" => array(
    ),
    "Interview" => array(
        "applicant_interview.interview_center_id#province_id#province_name" => "Interview Center Province",
        "applicant_interview.interview_center_id#interview_center_name" => "Interview Center Name",
        "applicant_interview.interview_id#room" => "Interview Room Number",
        "applicant_interview.interview_id#interview_date" => "Interview Date",
        "applicant_interview.interview_id#interview_start_time" => "Interview Start Time",
        "applicant_interview.interview_id#interview_end_time" => "Interview End Time",
        "applicant_interview.interview_type#program_name" => "Interview Type",
        "applicant_more_info.applicant_interview_attendance" => "Interview Attendance",
        "applicant_interview.interview_communication_and_maturity" => "Interview Communication and maturity",
        "applicant_interview.interview_motivation_for_cist" => "Interview for CIST",
        "applicant_interview.interview_affinities_with_computer_or_carear_plan" => "Interview Affinities with computer/ Carear plan",
    ),
    "Social Investigation" => array(
    ),
);
include($_SESSION["root_path"] . "/exams/exams_app_fields.inc");
require_once($_SESSION["root_path"] . "/general_queries.inc");
$programs = query_programs()->execute();
foreach ($programs as $p) {
    $app_display_names["Eligibility"]["applicant_eligibility.eligible/program_id/" . $p["program_id"]] = "Eligible for " . $p["program_name"];
    $app_display_names["Eligibility"]["applicant_interest.interested/program_id/" . $p["program_id"]] = "Interested by " . $p["program_name"];
}

class DisplayField {

    public $table;
    public $field;
    public $indirection;
    public $display_name;
    public $where;

    public function __construct($field, $name) {
        $i = strpos($field, "#");
        if ($i == false)
            $this->indirection = null;
        else {
            $this->indirection = substr($field, $i + 1);
            $field = substr($field, 0, $i);
        }
        $i = strpos($field, "/");
        if ($i == false)
            $this->where = null;
        else {
            $s = explode("/", substr($field, $i + 1));
            $field = substr($field, 0, $i);
            $this->where = array();
            for ($i = 0; $i < count($s); $i += 2)
                $this->where[$s[$i]] = $s[$i + 1];
        }
        $i = strpos($field, ".");
        $this->table = substr($field, 0, $i);
        $this->field = substr($field, $i + 1);
        $this->display_name = $name;
    }

    public function get_show_checkbox_name() {
        $name = "field__" . $this->table . "$" . $this->field . ($this->indirection <> null ? "$" . str_replace("#", "$", $this->indirection) : "");
        if ($this->where <> null) {
            foreach ($this->where as $key => $value)
                $name .= "$" . $key . "$" . $value;
        }
        return $name;
    }

    public function get_edit_checkbox_name() {
        return $this->get_show_checkbox_name() . "__edit";
    }

    public function get_filter_name() {
        return $this->table . "$" . $this->field . ($this->indirection <> null ? "$" . str_replace("#", "$", $this->indirection) : "");
    }

    function is_editable() {
        global $app_fields;
        $type = $app_fields[$this->table][$this->field];
        if ($type == 'KEY' || $type == 'KEY_AI')
            return false;
        if ($this->is_linked())
            return false;
        if ($this->table == "exam")
            return false;
        if ($this->table == "interview")
            return false;
        return true;
    }

    function is_linked() {
        if ($this->indirection == null)
            return false;
        if (strpos($this->indirection, "#") > 0)
            return true;
        global $app_display_names;
        foreach ($app_display_names as $section_name => $section_fields)
            foreach ($section_fields as $field => $name) {
                $f2 = new DisplayField($field, $name);
                if ($f2->table == $this->table && $f2->field == $this->field) {
                    if ($f2->indirection == $this->indirection)
                        continue;
                    if (strpos($f2->indirection, "#") > 0)
                        continue;
                    return true;
                }
            }
        return false;
    }

}

function get_indirect_fields_for_id($table, $id_field) {
    global $app_display_names;
    $result = array();
    $search = $table . "." . $id_field . "#";
    foreach ($app_display_names as $section => $section_fields) {
        foreach ($section_fields as $section_field_name => $section_field_type) {
            if (strlen($section_field_name) > strlen($search) && substr($section_field_name, 0, strlen($search)) == $search) {
                $indirect_field = substr($section_field_name, strlen($search));
                array_push($result, $indirect_field);
            }
        }
    }
    return $result;
}

?>